﻿using System;
namespace KouakKore
{
    public class KouakLauncher
    {
        private KouakServer ks;

        public void Stop()
        {
            try
            {
                ks.Stop();
            }
            catch (Exception e)
            {
            }
        }

        public void Launch(string[] Args)
        {
            KouakKore.KouakParse CommandLine = new KouakKore.KouakParse(Args);

            string cPath = @"\";
            int port = 80;
            KouakShareOption share = KouakShareOption.NotSet;
            KouakPathOption path = KouakPathOption.NotSet;

            if (CommandLine["r"] != null || CommandLine["relative"] != null)
            {
                path = KouakPathOption.RelativePath;
            }
            else if (CommandLine["a"] != null || CommandLine["absolute"] != null)
            {
                path = KouakPathOption.AbsolutePath;
            }

            if (CommandLine["f"] != null || CommandLine["file"] != null)
            {
                share = KouakShareOption.ShareFile;
            }
            else if (CommandLine["d"] != null || CommandLine["directory"] != null)
            {
                share = KouakShareOption.ShareDirectory;
            }

            if (CommandLine["p"] != null)
            {
                cPath = CommandLine["p"];
            }
            else if (CommandLine["path"] != null)
            {
                cPath = CommandLine["path"];
            }

            if (share == KouakShareOption.NotSet) share = parseFileOrDir(cPath);
            if (path == KouakPathOption.NotSet) path = parsePath(cPath);

            if (CommandLine["port"] != null)
            {
                port = getPort(CommandLine["port"]);
                if (port == 0)
                    port = 80;
            }

            try
            {
                Console.WriteLine("Share : {0} \nMode : {1} \nURIKind : {2} \nPort : {3}",cPath, share, path, port);
                ks = new KouakServer(cPath, share, path, port);
            }
            catch(Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }

        private static KouakPathOption parsePath(string path)
        {
            if (path.Length >= 3 && path[1] == ':' && (path[2] == '\\' || path[2] == '/'))
                return KouakPathOption.AbsolutePath;
            return KouakPathOption.RelativePath;
        }

        private static KouakShareOption parseFileOrDir(string path)
        {
            if (path.EndsWith("\\") || path.EndsWith("/"))
                return KouakShareOption.ShareDirectory;
            return KouakShareOption.ShareFile;
        }

        private static int getPort(string port)
        {
            try
            {
                int t = Convert.ToInt32(port);
                if (t < 0 || t >= 65535)
                    return 0;
                return t;
            }
            catch (Exception e)
            {
                return 0;
            }
        }
    }
}
